(function () {
    var io = ppt.io;
    var EVENT_CLICK = Laya.Event.CLICK;

    var SCENE_NAME = {
        "home" : "大厅",
        "waiting" : "匹配模式",
        "matching" : "竞技模式",
        "props" : "背包",
        "recharge" : "宝石充值",
        "role" : "角色",
        "roledetails" : "角色详情",
        "shop" : "商城",
    }

    function Header() {
        Header.super(this);

        this.showMail = false;

        this.init();
    }
    Laya.class(Header, "Libs.Header", ui.Public.HeaderUI);
    var _proto = Header.prototype;
    _proto.init = function () {
        this.y = -this.height;

        var mask = new Laya.Sprite();
            mask.loadImage("res/public/avatar_mask.png", 0, 0, this.avatar.width, this.avatar.height);
        this.avatar.mask = mask;

        io.register({
            "game::enter" : this.update,
            "mail::getMail" : this.getMail
        }, this);

        this.bindEvent();

        io.emit({"cmd" : "mail::getMail"});
    }
    _proto.bindEvent = function () {
        this.userBox.on(EVENT_CLICK, this, function () {
            ppt.director.popScene(new Alert.PersonalInfo);
        });
        this.back.on(EVENT_CLICK, this, this.backScene);
        this.rechargeBtn.on(EVENT_CLICK, this, function () {
            if(GM.appStorePay == 1 && (window.wltgame && wltgame.suportStorePay == 1) || GM.appStorePay == 2){
                location.href = "/?act=ios_applepay&st=applepay_game_vertical&gameId=" + GM.gameId + "&redirect_uri=" + redirect_uri;
            }else{
                ppt.director.runScene(new Scene.Recharge);
            }
        });
        this.messageBtn.on(EVENT_CLICK, this, function () {
            this.showMail = true;
            io.emit({"cmd" : "mail::getMail"});
        });
        this.setBtn.on(EVENT_CLICK, this, function () {
            ppt.director.popScene(new Alert.SetUp, {
                shadowClose : true
            });
        });
    }
    _proto.update = function (data) {
        this.avatar.skin = data.avatar == "" ? USER_DEFAULT_AVATAR : data.avatar;
        this.userName.text = utils.cutStr(data.name, 10);
        this.level.text = data.lvl;

        var progress = data.exp / data.nextExp;
        if(data.exp == 0){
            this.levelNum.visible = false;
        }else{
            this.levelNum.visible = true;
            this.levelNum.width = progress * 134;
        }

        this.coinMoney.text = utils.cutStr(data.coin, 8);
        this.starsMoney.text = utils.cutStr(data.honor, 8);
        this.diamondMoney.text = utils.cutStr(data.diamond, 7);
    }
    _proto.getMail = function (data) {
        if(data.length == 0){
            this.message.visible = false;
            this.showMail && ppt.director.popScene(new Alert.Mail(data));
        }else{
            this.message.visible = true;
            this.showMail && ppt.director.popScene(new Alert.Mail(data));
        }
        this.showMail = false;
    }
    _proto.backScene = function () {
        switch(this.sceneName){
            case "roledetails":
                ppt.director.runScene(new Scene.Role);
                break;
            case "waiting":
                // if(PVP_UNBIND == false){
                //     io.emit({"cmd" : "game::exit"});
                //     ppt.director.runScene(new Scene.CombaMode);
                // }
                io.emit({"cmd" : "game::exit"});
                break;
            default:
                ppt.director.runScene(new Scene.Home);
        }
    }
    _proto.enter = function (sceneName, data) {
        console.log("Header Enter", Date.now());
        io.emit({"cmd" : "game::enter"});

        this.sceneName = sceneName;

        this.controllerBox.visible = false;
        this.userBox.visible = false;
        this.rechargeBtn.visible = true;
        this.extraBox.visible = false;
        switch(sceneName){
            case "home":
                this.userBox.visible = true;
                this.extraBox.visible = true;
                break;
            case "recharge":
            case "waiting":
                this.controllerBox.visible = true;
                this.rechargeBtn.visible = false;
                break;
            default:
                this.controllerBox.visible = true;
                break;
        }
        this.scene_name.text = data ? data + SCENE_NAME[sceneName] : SCENE_NAME[sceneName];
        this.accountBox.x = sceneName == "home" ? 320 : 500;
        Laya.Tween.to(this, {y : 0}, 300, Laya.Ease.circIn);
    }
    _proto.exit = function () {
        this.y = -this.height;
        this.removeSelf();
    }
})();